<?php

namespace App\User\Models;

use App\Models\ApiSoftDeletes;
use App\Models\BaseModel;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Laravel\Lumen\Auth\Authorizable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class UserInfoModel extends BaseModel implements AuthenticatableContract, AuthorizableContract,JWTSubject
{
    use Authenticatable, Authorizable, ApiSoftDeletes;

    protected $table = 'user_info';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_name','real_name','phone','password','salt','last_login_time','status','role_id'
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
    ];

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [
            'user_name' => $this->user_name,
            'user_id' => $this->id,
        ];
    }

    public function getUserByName($userName)
    {
        $where = [];
        $where['user_name'] = $userName;
        $where['status'] = 0;
        $data = $this->db('a')
            ->where($where)->selectRaw('a.*')->first();
        return (array)$data;
    }

    public function getUserByOpenId($openId)
    {
        $where = [];
        $where['open_id'] = $openId;
        $where['status'] = 0;
        $data = $this->db('a')
            ->where($where)->selectRaw('a.*')->first();
        return (array)$data;
    }

    public function saveUserAuthInfo($openId, $authInfo)
    {
        $where = [];
        $where['open_id'] = $openId;
        $where['status'] = 0;
        return $this->updateData($authInfo, $where);
    }

    public function saveUser($userData)
    {
        return $this->db()->insertGetId($userData);
    }
}
